#tag Window
Begin Window Window1
   BackColor       =   16777215
   Backdrop        =   ""
   BalloonHelp     =   ""
   CloseButton     =   True
   Composite       =   False
   Frame           =   0
   FullScreen      =   False
   HasBackColor    =   False
   Height          =   314
   ImplicitInstance=   True
   LiveResize      =   False
   MacProcID       =   0
   MaxHeight       =   32000
   MaximizeButton  =   False
   MaxWidth        =   32000
   MenuBar         =   201791487
   MenuBarVisible  =   True
   MinHeight       =   64
   MinimizeButton  =   True
   MinWidth        =   64
   Placement       =   0
   Resizeable      =   False
   Title           =   "NumPad"
   Visible         =   True
   Width           =   300
   Begin EditField calcDisplay
      AcceptTabs      =   ""
      Alignment       =   0
      AutoDeactivate  =   True
      BackColor       =   16777215
      Bold            =   ""
      Border          =   True
      DataField       =   ""
      DataSource      =   ""
      Enabled         =   True
      Format          =   ""
      Height          =   36
      HelpTag         =   ""
      Index           =   -2147483648
      InitialParent   =   ""
      Italic          =   ""
      Left            =   12
      LimitText       =   0
      LockBottom      =   ""
      LockLeft        =   ""
      LockRight       =   ""
      LockTop         =   ""
      Mask            =   ""
      Multiline       =   ""
      Password        =   ""
      ReadOnly        =   ""
      Scope           =   0
      ScrollbarHorizontal=   ""
      ScrollbarVertical=   True
      Styled          =   ""
      TabIndex        =   0
      TabPanelIndex   =   0
      Text            =   ""
      TextColor       =   0
      TextFont        =   "System"
      TextSize        =   0
      Top             =   8
      Underline       =   ""
      UseFocusRing    =   True
      Visible         =   True
      Width           =   276
      BehaviorIndex   =   0
   End
   Begin PushButton numButton
      AutoDeactivate  =   True
      Bold            =   ""
      Cancel          =   ""
      Caption         =   0
      Default         =   ""
      Enabled         =   True
      Height          =   40
      HelpTag         =   ""
      Index           =   0
      InitialParent   =   ""
      Italic          =   ""
      Left            =   44
      LockBottom      =   ""
      LockLeft        =   ""
      LockRight       =   ""
      LockTop         =   ""
      Scope           =   0
      TabIndex        =   1
      TabPanelIndex   =   0
      TextFont        =   "System"
      TextSize        =   0
      Top             =   254
      Underline       =   ""
      Visible         =   True
      Width           =   40
      BehaviorIndex   =   1
   End
   Begin PushButton numButton
      AutoDeactivate  =   True
      Bold            =   ""
      Cancel          =   ""
      Caption         =   1
      Default         =   ""
      Enabled         =   True
      Height          =   40
      HelpTag         =   ""
      Index           =   1
      InitialParent   =   ""
      Italic          =   ""
      Left            =   44
      LockBottom      =   ""
      LockLeft        =   ""
      LockRight       =   ""
      LockTop         =   ""
      Scope           =   0
      TabIndex        =   2
      TabPanelIndex   =   0
      TextFont        =   "System"
      TextSize        =   0
      Top             =   202
      Underline       =   ""
      Visible         =   True
      Width           =   40
      BehaviorIndex   =   1
   End
   Begin PushButton numButton
      AutoDeactivate  =   True
      Bold            =   ""
      Cancel          =   ""
      Caption         =   2
      Default         =   ""
      Enabled         =   True
      Height          =   40
      HelpTag         =   ""
      Index           =   2
      InitialParent   =   ""
      Italic          =   ""
      Left            =   96
      LockBottom      =   ""
      LockLeft        =   ""
      LockRight       =   ""
      LockTop         =   ""
      Scope           =   0
      TabIndex        =   3
      TabPanelIndex   =   0
      TextFont        =   "System"
      TextSize        =   0
      Top             =   202
      Underline       =   ""
      Visible         =   True
      Width           =   40
      BehaviorIndex   =   1
   End
   Begin PushButton numButton
      AutoDeactivate  =   True
      Bold            =   ""
      Cancel          =   ""
      Caption         =   3
      Default         =   ""
      Enabled         =   True
      Height          =   40
      HelpTag         =   ""
      Index           =   3
      InitialParent   =   ""
      Italic          =   ""
      Left            =   148
      LockBottom      =   ""
      LockLeft        =   ""
      LockRight       =   ""
      LockTop         =   ""
      Scope           =   0
      TabIndex        =   4
      TabPanelIndex   =   0
      TextFont        =   "System"
      TextSize        =   0
      Top             =   202
      Underline       =   ""
      Visible         =   True
      Width           =   40
      BehaviorIndex   =   1
   End
   Begin PushButton numButton
      AutoDeactivate  =   True
      Bold            =   ""
      Cancel          =   ""
      Caption         =   4
      Default         =   ""
      Enabled         =   True
      Height          =   40
      HelpTag         =   ""
      Index           =   4
      InitialParent   =   ""
      Italic          =   ""
      Left            =   44
      LockBottom      =   ""
      LockLeft        =   ""
      LockRight       =   ""
      LockTop         =   ""
      Scope           =   0
      TabIndex        =   5
      TabPanelIndex   =   0
      TextFont        =   "System"
      TextSize        =   0
      Top             =   154
      Underline       =   ""
      Visible         =   True
      Width           =   40
      BehaviorIndex   =   1
   End
   Begin PushButton numButton
      AutoDeactivate  =   True
      Bold            =   ""
      Cancel          =   ""
      Caption         =   5
      Default         =   ""
      Enabled         =   True
      Height          =   40
      HelpTag         =   ""
      Index           =   5
      InitialParent   =   ""
      Italic          =   ""
      Left            =   96
      LockBottom      =   ""
      LockLeft        =   ""
      LockRight       =   ""
      LockTop         =   ""
      Scope           =   0
      TabIndex        =   6
      TabPanelIndex   =   0
      TextFont        =   "System"
      TextSize        =   0
      Top             =   154
      Underline       =   ""
      Visible         =   True
      Width           =   40
      BehaviorIndex   =   1
   End
   Begin PushButton numButton
      AutoDeactivate  =   True
      Bold            =   ""
      Cancel          =   ""
      Caption         =   6
      Default         =   ""
      Enabled         =   True
      Height          =   40
      HelpTag         =   ""
      Index           =   6
      InitialParent   =   ""
      Italic          =   ""
      Left            =   148
      LockBottom      =   ""
      LockLeft        =   ""
      LockRight       =   ""
      LockTop         =   ""
      Scope           =   0
      TabIndex        =   7
      TabPanelIndex   =   0
      TextFont        =   "System"
      TextSize        =   0
      Top             =   154
      Underline       =   ""
      Visible         =   True
      Width           =   40
      BehaviorIndex   =   1
   End
   Begin PushButton numButton
      AutoDeactivate  =   True
      Bold            =   ""
      Cancel          =   ""
      Caption         =   7
      Default         =   ""
      Enabled         =   True
      Height          =   40
      HelpTag         =   ""
      Index           =   7
      InitialParent   =   ""
      Italic          =   ""
      Left            =   44
      LockBottom      =   ""
      LockLeft        =   ""
      LockRight       =   ""
      LockTop         =   ""
      Scope           =   0
      TabIndex        =   8
      TabPanelIndex   =   0
      TextFont        =   "System"
      TextSize        =   0
      Top             =   102
      Underline       =   ""
      Visible         =   True
      Width           =   40
      BehaviorIndex   =   1
   End
   Begin PushButton numButton
      AutoDeactivate  =   True
      Bold            =   ""
      Cancel          =   ""
      Caption         =   8
      Default         =   ""
      Enabled         =   True
      Height          =   40
      HelpTag         =   ""
      Index           =   8
      InitialParent   =   ""
      Italic          =   ""
      Left            =   96
      LockBottom      =   ""
      LockLeft        =   ""
      LockRight       =   ""
      LockTop         =   ""
      Scope           =   0
      TabIndex        =   9
      TabPanelIndex   =   0
      TextFont        =   "System"
      TextSize        =   0
      Top             =   102
      Underline       =   ""
      Visible         =   True
      Width           =   40
      BehaviorIndex   =   1
   End
   Begin PushButton numButton
      AutoDeactivate  =   True
      Bold            =   ""
      Cancel          =   ""
      Caption         =   9
      Default         =   ""
      Enabled         =   True
      Height          =   40
      HelpTag         =   ""
      Index           =   9
      InitialParent   =   ""
      Italic          =   ""
      Left            =   148
      LockBottom      =   ""
      LockLeft        =   ""
      LockRight       =   ""
      LockTop         =   ""
      Scope           =   0
      TabIndex        =   10
      TabPanelIndex   =   0
      TextFont        =   "System"
      TextSize        =   0
      Top             =   102
      Underline       =   ""
      Visible         =   True
      Width           =   40
      BehaviorIndex   =   1
   End
   Begin PushButton opButton
      AutoDeactivate  =   True
      Bold            =   ""
      Cancel          =   ""
      Caption         =   "="
      Default         =   ""
      Enabled         =   True
      Height          =   88
      HelpTag         =   ""
      Index           =   0
      InitialParent   =   ""
      Italic          =   ""
      Left            =   200
      LockBottom      =   ""
      LockLeft        =   ""
      LockRight       =   ""
      LockTop         =   ""
      Scope           =   0
      TabIndex        =   11
      TabPanelIndex   =   0
      TextFont        =   "System"
      TextSize        =   0
      Top             =   206
      Underline       =   ""
      Visible         =   True
      Width           =   40
      BehaviorIndex   =   2
   End
   Begin PushButton opButton
      AutoDeactivate  =   True
      Bold            =   ""
      Cancel          =   ""
      Caption         =   "+"
      Default         =   ""
      Enabled         =   True
      Height          =   82
      HelpTag         =   ""
      Index           =   1
      InitialParent   =   ""
      Italic          =   ""
      Left            =   200
      LockBottom      =   ""
      LockLeft        =   ""
      LockRight       =   ""
      LockTop         =   ""
      Scope           =   0
      TabIndex        =   12
      TabPanelIndex   =   0
      TextFont        =   "System"
      TextSize        =   0
      Top             =   108
      Underline       =   ""
      Visible         =   True
      Width           =   40
      BehaviorIndex   =   2
   End
   Begin PushButton opButton
      AutoDeactivate  =   True
      Bold            =   ""
      Cancel          =   ""
      Caption         =   "-"
      Default         =   ""
      Enabled         =   True
      Height          =   40
      HelpTag         =   ""
      Index           =   2
      InitialParent   =   ""
      Italic          =   ""
      Left            =   200
      LockBottom      =   ""
      LockLeft        =   ""
      LockRight       =   ""
      LockTop         =   ""
      Scope           =   0
      TabIndex        =   13
      TabPanelIndex   =   0
      TextFont        =   "System"
      TextSize        =   0
      Top             =   56
      Underline       =   ""
      Visible         =   True
      Width           =   40
      BehaviorIndex   =   2
   End
   Begin PushButton opButton
      AutoDeactivate  =   True
      Bold            =   ""
      Cancel          =   ""
      Caption         =   "*"
      Default         =   ""
      Enabled         =   True
      Height          =   40
      HelpTag         =   ""
      Index           =   3
      InitialParent   =   ""
      Italic          =   ""
      Left            =   148
      LockBottom      =   ""
      LockLeft        =   ""
      LockRight       =   ""
      LockTop         =   ""
      Scope           =   0
      TabIndex        =   14
      TabPanelIndex   =   0
      TextFont        =   "System"
      TextSize        =   0
      Top             =   56
      Underline       =   ""
      Visible         =   True
      Width           =   40
      BehaviorIndex   =   2
   End
   Begin PushButton numButton
      AutoDeactivate  =   True
      Bold            =   ""
      Cancel          =   ""
      Caption         =   "."
      Default         =   ""
      Enabled         =   True
      Height          =   40
      HelpTag         =   ""
      Index           =   10
      InitialParent   =   ""
      Italic          =   ""
      Left            =   96
      LockBottom      =   ""
      LockLeft        =   ""
      LockRight       =   ""
      LockTop         =   ""
      Scope           =   0
      TabIndex        =   15
      TabPanelIndex   =   0
      TextFont        =   "System"
      TextSize        =   32
      Top             =   254
      Underline       =   ""
      Visible         =   True
      Width           =   40
      BehaviorIndex   =   1
   End
   Begin PushButton opButton
      AutoDeactivate  =   True
      Bold            =   ""
      Cancel          =   ""
      Caption         =   "/"
      Default         =   ""
      Enabled         =   True
      Height          =   40
      HelpTag         =   ""
      Index           =   4
      InitialParent   =   ""
      Italic          =   ""
      Left            =   96
      LockBottom      =   ""
      LockLeft        =   ""
      LockRight       =   ""
      LockTop         =   ""
      Scope           =   0
      TabIndex        =   16
      TabPanelIndex   =   0
      TextFont        =   "System"
      TextSize        =   0
      Top             =   56
      Underline       =   ""
      Visible         =   True
      Width           =   40
      BehaviorIndex   =   2
   End
   Begin PushButton opButton
      AutoDeactivate  =   True
      Bold            =   ""
      Cancel          =   ""
      Caption         =   "C"
      Default         =   ""
      Enabled         =   True
      Height          =   40
      HelpTag         =   ""
      Index           =   5
      InitialParent   =   ""
      Italic          =   ""
      Left            =   44
      LockBottom      =   ""
      LockLeft        =   ""
      LockRight       =   ""
      LockTop         =   ""
      Scope           =   0
      TabIndex        =   17
      TabPanelIndex   =   0
      TextFont        =   "System"
      TextSize        =   0
      Top             =   56
      Underline       =   ""
      Visible         =   True
      Width           =   40
      BehaviorIndex   =   2
   End
   Begin PushButton backButton
      AutoDeactivate  =   True
      Bold            =   ""
      Cancel          =   ""
      Caption         =   "<"
      Default         =   ""
      Enabled         =   True
      Height          =   40
      HelpTag         =   ""
      Index           =   -2147483648
      InitialParent   =   ""
      Italic          =   ""
      Left            =   148
      LockBottom      =   ""
      LockLeft        =   ""
      LockRight       =   ""
      LockTop         =   ""
      Scope           =   0
      TabIndex        =   18
      TabPanelIndex   =   0
      TextFont        =   "System"
      TextSize        =   0
      Top             =   254
      Underline       =   ""
      Visible         =   True
      Width           =   40
      BehaviorIndex   =   3
   End
End
#tag EndWindow

#tag WindowCode
	#tag Property, Flags = &h0
		accumulator As Double
	#tag EndProperty

	#tag Property, Flags = &h0
		opInProgress As String
	#tag EndProperty


#tag EndWindowCode

#tag Events calcDisplay
	#tag Event
		Sub Open()
		  me.Alignment = EditField.AlignRight
		End Sub
	#tag EndEvent
#tag EndEvents
#tag Events numButton
	#tag Event
		Sub Action()
		  calcDisplay.SelText = me.Caption
		End Sub
	#tag EndEvent
#tag EndEvents
#tag Events opButton
	#tag Event
		Sub Action()
		  // When you press a Operation button, it performs an action on the accumulator
		  // depending on which was the last Operation button pressed.
		  // Please note, all arithmetic operations are performed in order, there is no operator precedence.
		  
		  Dim currentNumber As Double
		  Dim TestString As String
		  
		  currentNumber = CDbl(calcDisplay.Text)
		  
		  select case opInProgress
		  case "","C", "="
		    accumulator = currentNumber
		  case "+"
		    accumulator = accumulator + currentNumber
		  case "-"
		    accumulator = accumulator - currentNumber
		  case "*"
		    accumulator = accumulator * currentNumber
		  case "/"
		    if currentNumber = 0 Then
		      MsgBox "division by zero error"
		      accumulator = 0
		      currentNumber = 0
		    Else
		      accumulator = accumulator / currentNumber
		    end if
		  end select
		  
		  self.Title = "NumPad "
		  select case me.Caption
		  case "="
		    // check for scientific notation in result - we don't want it
		    TestString = CStr(accumulator)
		    if InStr(0,TestString,"e") > 0 Then
		      TestString = Format(accumulator,"0.0#########")
		    end if
		    // Show the result
		    calcDisplay.Text = TestString
		    //Select the rsult, so the next number will replace it
		    calcDisplay.SelStart = 0
		    calcDisplay.SelLength = Len(calcDisplay.Text)
		  case "C"
		    // Empty both numbers
		    accumulator = 0
		    currentNumber = 0
		    calcDisplay.Text = ""
		  case else
		    // this operation is midway through a calculation
		    // blank display so they're not confused by existing display
		    calcDisplay.Text = ""
		    self.Title = self.Title + CStr(accumulator) + " " + me.Caption
		  end select
		  
		  
		  //Tell it what to do, next time an op button ( like "=" ) is pressed
		  
		  opInProgress = me.Caption
		End Sub
	#tag EndEvent
#tag EndEvents
#tag Events backButton
	#tag Event
		Sub Action()
		  Dim pos as Integer
		  if calcDisplay.SelLength > 0 Then
		    calcDisplay.SelText = ""
		  Else
		    pos = calcDisplay.SelStart
		    if pos > 0 Then
		      calcDisplay.SelStart = pos -1
		      calcDisplay.SelLength = 1
		      calcDisplay.SelText = ""
		    end if
		  end if
		End Sub
	#tag EndEvent
#tag EndEvents
